package observer.wechat;

import java.util.ArrayList;
import java.util.List;

public class Publisher {

	private List<Subscriber> subscribers = new ArrayList<>();

	public void subscribe(Subscriber suscriber) {
		this.subscribers.add(suscriber);
	}

	public void unsubscribe(Subscriber suscriber) {
		if (this.subscribers.contains(suscriber)) {
			this.subscribers.remove(suscriber);
		}
	}

	public void notifySuscribers(String publisher, String articleName) {
		for (Subscriber s : this.subscribers) {
			s.receive(publisher, articleName);
		}
	}
}

class WechatPublisher extends Publisher {
	private String username;

	public WechatPublisher(String username) {
		this.username = username;
	}

	public void publish(String articleName) {
		System.out.println(String.format("<%s> 发布文章<%s>", this.username, articleName));
		notifySuscribers(this.username, articleName);
	}
}